package com.rc.saas.tenant.service.tenant;

import com.rc.saas.tenant.common.constant.WebConstant;
import com.rc.saas.tenant.mapper.tenant.TenantMenuMapper;
import com.rc.saas.tenant.mapper.operator.TenantOperatorMenuMapper;
import com.rc.saas.tenant.mapper.tenant.TenantRoleMenuMapper;
import com.rc.saas.tenant.model.operator.TenantOperator;
import com.rc.saas.tenant.model.tenant.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sven on 2018-04-17
 */
@Service
public class TenantMenuServiceImpl implements TenantMenuService {
    @Autowired
    private TenantMenuMapper tenantMenuMapper;

    @Autowired
    private TenantOperatorMenuMapper tenantOperatorMenuMapper;

    @Autowired
    private TenantRoleMenuMapper tenantRoleMenuMapper;

    @Override
    public TenantMenuMapper getMapper() {
        return tenantMenuMapper;
    }

    @Override
    public void deleteMenu(Long id) {
        if (id > 0) {
            //删除菜单树
            tenantMenuMapper.deleteTreeByMenu(id);

            //删除角色的菜单权限
            tenantOperatorMenuMapper.deleteByMenu(id);
        }
    }

    @Override
    public List<TenantMenu> listMenuByRole(Long roleId) {
        return tenantMenuMapper.listMenuByRole(roleId);
    }

    @Override
    public List<TenantMenu> listMenuByTopMenuId(TenantUser tenantUser, Long topMenuId) {
        String tenantCode = tenantUser.getTenantCode();

        if (!tenantUser.getUserType().equals(WebConstant.STATUS_ZERO)) {
            TenantRoleMenuExample tenantRoleMenuExample = new TenantRoleMenuExample();
            tenantRoleMenuExample.createCriteria().andTenantRoleIdEqualTo(tenantUser.getTenantRoleId());
            List<TenantRoleMenu> tenantRoleMenuList = tenantRoleMenuMapper.selectByExample(tenantRoleMenuExample);
            if (tenantRoleMenuList == null || tenantRoleMenuList.size() < 1) {
                return new ArrayList<TenantMenu>();
            }

            return tenantMenuMapper.listMenuByTopMenuId(tenantCode, topMenuId, tenantRoleMenuList);
        } else if (tenantUser.getUserType().equals(WebConstant.STATUS_ZERO)) {
            return tenantMenuMapper.listMenuByTopMenuId(tenantCode, topMenuId, null);
        }
        return null;
    }

    @Override
    public TenantMenu selectMenuByPermission(Long tenantMenuId, String permission) {
        TenantMenuExample example = new TenantMenuExample();
        example.createCriteria().andTenantMenuIdEqualTo(tenantMenuId).andPermissionEqualTo(permission);
        List<TenantMenu> list = tenantMenuMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<TenantMenu> listMenuByOperator() {
        return tenantMenuMapper.listMenuByOperator();
    }

    @Override
    public List<TenantMenu> listMenuByEnterprise() {
        return tenantMenuMapper.listMenuByEnterprise();
    }

    @Override
    public List<TenantMenu> listOperatoMenuByTopMenuId(Long topMenuId) {
        return tenantMenuMapper.listOperatorMenuByTopMenuId(topMenuId);
    }

    @Override
    public List<TenantMenu> listOperatoMenuByTopMenuIdAndOperator(String tenantOperatorCode, Long topMenuId) {
        return tenantMenuMapper.listOperatoMenuByTopMenuIdAndOperator(tenantOperatorCode, topMenuId);
    }

    @Override
    public List<TenantMenu> listEnterpriseMenuByTopMenuId(Long topMenuId) {
        return tenantMenuMapper.listEnterpriseMenuByTopMenuId(topMenuId);
    }

    //@Override
    //public List<TenantMenu> selectOperatoMenuByTopMenuId(String tenantCode, Long topMenuId) {
    //    return tenantMenuMapper.selectOperatorMenuByTopMenuId2(tenantCode, topMenuId);
    //}

    @Override
    public List<TenantMenu> listTopMenuByTenant(TenantUser user) {
        if (!user.getUserType().equals(WebConstant.STATUS_ZERO)) {
            TenantRoleMenuExample tenantRoleMenuExample = new TenantRoleMenuExample();
            tenantRoleMenuExample.createCriteria().andTenantRoleIdEqualTo(user.getTenantRoleId());
            List<TenantRoleMenu> tenantRoleMenuList = tenantRoleMenuMapper.selectByExample(tenantRoleMenuExample);
            if (tenantRoleMenuList == null || tenantRoleMenuList.size() < 1) {
                return new ArrayList<TenantMenu>();
            }

            List<TenantMenu> tenantMenuList = tenantMenuMapper.listTopMenuByTenantRoleMenu(tenantRoleMenuList);
            return tenantMenuList;
        } else if (user.getUserType().equals(WebConstant.STATUS_ZERO)) {
            TenantMenuExample example = new TenantMenuExample();
            //状态 0=显示 1=隐藏
            example.createCriteria().andParentIdEqualTo(0L).andUserTypeEqualTo("0").andStatusEqualTo("0");
            example.setOrderByClause("sort");
            return tenantMenuMapper.selectByExample(example);
        }
        return new ArrayList<TenantMenu>();
    }

    @Override
    public List<TenantMenu> listTopMenuByOperator() {
        return tenantMenuMapper.listTopMenuByOperator();
    }

    @Override
    public List<TenantMenu> listTopMenuByEnterprise() {
        return tenantMenuMapper.listTopMenuByEnterprise();
    }
}
